//
//  JWAlertChoseButsView.m
//  jinwang
//
//  Created by jzb on 2018/3/7.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWAlertChoseButsView.h"
@interface JWAlertChoseButsView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UIButton *_GradeBGButton;//年级背景button
    UIView *_NianJiXuanZeBackgroundView;//年级背景
    
    UIButton *_ContentBGButton;//学科背景button
    
    UIView *_XueKeChoseBackgroundView;//学科背景
    
    UICollectionView *_collectionView;//学科collection
    
    UIPickerView *_NianJiXuanZePickerView;//年级pickerView
    
    NSMutableArray *_postArr;//
    
    NSArray *_GradeNameArr;
    
    NSArray *_ContentNameArr;
    
    NSInteger ContentIndex;
    
    NSString *_Professiona;
    
}

@end
@implementation JWAlertChoseButsView
//单元格复用的标识
static NSString *identifier = @"collection_cell";


- (void)setDataList:(NSArray *)dataList{
    
    _dataList = dataList;
    
    [self XueKeScrollViewBuild];
}

- (void)XueKeScrollViewBuild{
    
    _postArr = [NSMutableArray new];
    
    self.backgroundColor = [UIColor whiteColor];
    //I.创建
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.itemSize = CGSM((750-160-60)/3, 60);
    
    layout.minimumLineSpacing = 10;
    
    layout.minimumInteritemSpacing = 10;
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
     _collectionView = [[UICollectionView alloc]initWithFrame:CGRM(0, 0, 750-160, 298) collectionViewLayout:layout];
    
    if ([_StrOrArr isEqualToString:@"arr"]) {
        
         _collectionView = [[UICollectionView alloc]initWithFrame:CGRM(0, 0, 750-160, 240) collectionViewLayout:layout];
        
        UILabel *tishiLabel = [[UILabel alloc]initWithFrame:CGRM(30, 250, 750-160, 30)];
        tishiLabel.text = @"*最多选择3个";
        tishiLabel.font = [UIFont systemFontOfSize:10];
        tishiLabel.textColor = kRGBColor(90, 90, 90);
        [self addSubview:tishiLabel];
        
    }else if ([_StrOrArr isEqualToString:@"jubao"]) {
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRM(0, 70, 750-160, 218) collectionViewLayout:layout];
        
        UILabel *tishiLabel = [[UILabel alloc]initWithFrame:CGRM(20, 20, 750-160, 40)];
        tishiLabel.text = @"请选择举报类型";
        tishiLabel.font = [UIFont systemFontOfSize:15];
        tishiLabel.textColor = kRGBColor(90, 90, 90);
        [self addSubview:tishiLabel];
    }
   
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    _collectionView.dataSource = self;
    
    _collectionView.delegate = self;
    
    _collectionView.allowsSelection = YES;
    
    _collectionView.allowsMultipleSelection = YES;
    
    [self addSubview:_collectionView];
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
    
    if ([_StrOrArr isEqualToString:@"jubao"]) {
        
        UIButton *quedingbutton = [[UIButton alloc]initWithFrame:CGRectMake(self.width/3, self.height - 55, self.width/3,35)];
        quedingbutton.backgroundColor = UIColorHex(0xEEA72B);
        [quedingbutton setTitle:@"提交" forState:UIControlStateNormal];
        [quedingbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        quedingbutton.tag = 1002;
        quedingbutton.layer.cornerRadius = 4;
        quedingbutton.layer.masksToBounds = YES;
        [quedingbutton addTarget:self action:@selector(xieyitiaokuanbuttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:quedingbutton];
        
    }else{
        UIView *hengline = [[UIView alloc]initWithFrame:CGRM(0, 400 - 102, 750-160, 2)];
        hengline.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:hengline];
        UIView *shuline = [[UIView alloc]initWithFrame:CGRM((750-160)/2-1, 400 - 100, 2, 100)];
        shuline.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:shuline];
        
        UIButton *quxiaobutton = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height - 50, self.width/2-0.5,50)];
        [quxiaobutton setTitle:@"取消" forState:UIControlStateNormal];
        [quxiaobutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [quxiaobutton addTarget:self action:@selector(xieyitiaokuanbuttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
        quxiaobutton.tag = 1001;
        [self addSubview:quxiaobutton];
        
        UIButton *quedingbutton = [[UIButton alloc]initWithFrame:CGRectMake(self.width/2-1, self.height - 50, self.width/2-0.5,50)];
        //    quedingbutton.backgroundColor = [UIColor orangeColor];
        [quedingbutton setTitle:@"确认" forState:UIControlStateNormal];
        [quedingbutton setTitleColor:kRGBColor(89, 176, 254) forState:UIControlStateNormal];
        quedingbutton.tag = 1002;
        [quedingbutton addTarget:self action:@selector(xieyitiaokuanbuttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:quedingbutton];
    }
}

#pragma mark --UICollectionViewDataSource

//单元格个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _dataList.count ;
    
}
//单元格内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //使用注册的单元格类 创建单元格
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
        cell.backgroundColor = [UIColor whiteColor];
    
    for (UIView *view in cell.subviews) {
        
        [view removeFromSuperview];
    }
    
        UILabel *label = [[UILabel alloc]initWithFrame:CGRM(50, 20, (750-160-40)/3, 40)];
    
    label.backgroundColor = [UIColor whiteColor];
    
    label.textAlignment = NSTextAlignmentLeft;
    
    label.font = [UIFont systemFontOfSize:14];
    
    label.textColor = kRGBColor(90, 90, 90);
    
    label.tag = 3001;
    
    [cell addSubview:label];
    
    label.text = _dataList[indexPath.item];
    
    UIButton *itemBut = [[UIButton alloc]initWithFrame:cell.bounds];
    
    [itemBut setImageEdgeInsets:UIEdgeInsetsMake(10, 0, 0, 60)];
    
    [itemBut setImage:[UIImage imageNamed:@"jw_grayYuan"] forState:UIControlStateNormal];
    
    [itemBut addTarget:self action:@selector(itemButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    itemBut.tag = 600+indexPath.item;
    
    [cell addSubview:itemBut];
    
    
//    if (UserInfo.course.length > 0 && [UserInfo.course isEqual:[NSNull null]] == NO) {
//
//        for (NSString *UserContentName in _xuekeNameArr) {
//
//            if ([UserContentName isEqualToString:_XueKeArr[indexPath.item]]) {
//
//                [itemBut setImage:[UIImage imageNamed:@"jw_sex_btn_sel.png"] forState:UIControlStateNormal];
//
//                itemBut.selected = YES;
    
//                [_xuekeButArr addObject:itemBut];
//
//                _xuekeChoseButArr = [[NSMutableArray alloc]initWithArray:_xuekeButArr];
//
//            }
//        }
//    }
    
    return cell;
}

#pragma mark ----UICollectionViewDelegateFlowLayout
//单元格距离collectionView 四周边缘的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    
    return UIEdgeInsetsMake(5, 5, 5, 5);
    
}
- (void)itemButtonOnClick:(UIButton *)sender{
    
    if ([_StrOrArr isEqualToString:@"arr"]) {
        
        sender.selected = !sender.selected;
        
        if (sender.selected == YES) {
            
            NSLog(@"-=-=-=>>>%@",_postArr);
            
            if (_postArr.count <= 2) {
                
                [sender setImage: [UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
                
                [_postArr addObject:_dataList[sender.tag - 600]];
                
            }else{
                
                [XHToast showBottomWithText:@"最多选择3个"];
            }
            
        }else{
            
            [sender setImage: [UIImage imageNamed:@"jw_grayYuan"] forState:UIControlStateNormal];
            
            [_postArr removeObject:_dataList[sender.tag-600]];
            
        }
    }else{
       
        for (int i = 0; i < _dataList.count; i++) {
            
            UIButton *manbutton = (UIButton *)[self viewWithTag:600 + i];
            [manbutton setImage:[UIImage imageNamed:@"jw_grayYuan"] forState:UIControlStateNormal];
        }
        
        [sender setImage: [UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
        
        _Professiona = _dataList[sender.tag - 600];
        
    }

}
- (void)xieyitiaokuanbuttonOnclick:(UIButton *)sender{
    
    [self hideView];
    if (sender.tag == 1001) {
        
        self.ProfessionaDateBlcok(_Professiona,_postArr);
        
    }else if (sender.tag == 1002){
        
        self.ProfessionaDateBlcok(_Professiona,_postArr);
        
    }
}

@end
